package {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.text.TextFormat;
  import flash.text.TextFormatAlign;

  public class Tooltip extends Sprite {

    private var bgColor:uint;
    private var textField:TextField;
    private var textFormat:TextFormat;
    private var w:Number;
    private var h:Number;

    public function Tooltip(bgColor:uint = 0x0, fgColor:uint = 0xffffff, alpha:Number = 1, width:Number = 100, height:Number = 100) {
      textField = new TextField();
      textField.autoSize = TextFieldAutoSize.CENTER;
      textField.textColor = fgColor;
      textField.x = 0;
      textField.y = 0;
      textField.width = w*.5;
      textField.height = h*.5;
      textField.mouseEnabled = false;
      addChild(textField);
      textFormat = new TextFormat();
      textFormat.font = "Arial";
      textFormat.size = width*.2;
      textFormat.align = TextFormatAlign.CENTER;
      this.bgColor = bgColor;
      this.alpha = alpha;
      this.w = width;
      this.h = height;
      this.mouseEnabled = false;
    }

    private function paint():void {
      var w:Number = Math.max(this.w, textField.width);
      var h:Number = Math.max(this.h, textField.height);
      graphics.clear();
      graphics.beginFill(bgColor);
      graphics.lineStyle(1, bgColor / 2);
      graphics.drawRect(-w*.5, -h*.5, w, h);
      graphics.endFill();
    }
    
    public function set text(t:String):void {
      textField.text = t;
      textField.setTextFormat(textFormat);
      textField.y = -textField.height*.5;
      paint();
    }
  }
}



